﻿using gt.dotnetcore.webapi.extensions.ApiResults;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace gt.dotnetcore.webapi.extensions.ApiException
{
    /// <summary>
    /// 异常拦截器
    /// </summary>
    public class DefaultExceptionFilterAttribute : ExceptionFilterAttribute
    {
        private readonly ILogger _logger;

        public DefaultExceptionFilterAttribute(ILogger<DefaultExceptionFilterAttribute> logger)
        {
            _logger = logger;
        }

        public override void OnException(ExceptionContext context)
        {
            var ex = context.Exception;
            _logger.LogError(ex, context.HttpContext.Request.GetClientId());
            context.Result = new ErrorResult(-999, "操作失败！");
        }
    }
}
